﻿Imports Publics
Imports System.Windows.Forms

Public Class FrmCapNhatKhachHang

    Private _KhachHangEdit As DM_KhachHang
    Public Property KhachHangEdit() As DM_KhachHang
        Get
            Return _KhachHangEdit
        End Get
        Set(ByVal value As DM_KhachHang)
            _KhachHangEdit = value
        End Set
    End Property

    Private _IDKhachHang As String
    Public Property IDKhachHang() As String
        Get
            Return _IDKhachHang
        End Get
        Set(ByVal value As String)
            _IDKhachHang = value
        End Set
    End Property

    Private _Action As String
    Public Property Action() As String
        Get
            Return _Action
        End Get
        Set(ByVal value As String)
            _Action = value
        End Set
    End Property

    Private Sub btnThoat_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnThoat.ItemClick
        If String.IsNullOrEmpty(txtMaKhachHang.Text.Trim) Then
            Me.Close()
            Exit Sub

        End If
        If MsgAskSaveData("Chưa ghi dữ liệu, bạn có muốn thoát không?") = vMsg.vReSult.Yes Then
            Me.Close()
        End If
    End Sub

    Private Sub FrmCapNhatKhachHang_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            btnThoat_ItemClick(Nothing, Nothing)
        End If
        'Ghi và đóng
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pSAVECLOSE, btnGhiVaDong) Then
            btnGhiVaDong_ItemClick(Nothing, Nothing)
        End If
    End Sub

    Private Sub FrmCapNhatKhachHang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        btnGhiVaDong.Caption = BtnSaveAndCloseCaption
        btnGhiVaThem.Caption = BtnSaveAndAddCaption
        btnThoat.Caption = BtnExitCaption
        lbPhimTat.Text = GetLabelPhimTatCoBanCapNhatDanhMuc()
        Select Case Action
            Case vCopyAction, vEditAction
                If KhachHangEdit IsNot Nothing Then
                    txtMaKhachHang.Text = KhachHangEdit.MaKhachHang
                    txtTenKhachHang.Text = KhachHangEdit.TenKhachHang
                    txtDiaChi.Text = KhachHangEdit.DiaChi
                    txtDienThoai.Text = KhachHangEdit.DienThoai
                    txtMaSoThue.Text = KhachHangEdit.MaSoThue
                    txtGhiChu.Text = KhachHangEdit.GhiChu
                    txtDiaChiLH.Text = KhachHangEdit.LH_DiaChi
                    txtDienThoaiLH.Text = KhachHangEdit.LH_DienThoai
                    txtHoVaTenLH.Text = KhachHangEdit.LH_HoTen
                    txtEmailLH.Text = KhachHangEdit.LH_Email
                End If
        End Select
    End Sub

    Private Sub btnGhiVaDong_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnGhiVaDong.ItemClick
        If SaveData() Then
            Me.Close()
        End If
    End Sub

    Private Sub btnGhiVaThem_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnGhiVaThem.ItemClick
        If SaveData() Then
            txtMaKhachHang.Text = ""
        End If
    End Sub

    Private Function SaveData()
        Try
            Dim strError = CheckSave()
            If strError <> "" Then
                MsgInfo(strError)
                Return False
            End If

            Select Case Action
                Case vAddAction, vCopyAction
                    Dim objKhachHangAdd As New DM_KhachHang
                    objKhachHangAdd.ID = CreateID()
                    IDKhachHang = objKhachHangAdd.ID
                    objKhachHangAdd.MaKhachHang = txtMaKhachHang.Text.Trim
                    objKhachHangAdd.TenKhachHang = txtTenKhachHang.Text.Trim
                    objKhachHangAdd.DiaChi = txtDiaChi.Text.Trim
                    objKhachHangAdd.DienThoai = txtDienThoai.Text.Trim
                    objKhachHangAdd.MaSoThue = txtMaSoThue.Text.Trim
                    objKhachHangAdd.GhiChu = txtGhiChu.Text.Trim
                    objKhachHangAdd.LH_HoTen = txtHoVaTenLH.Text.Trim
                    objKhachHangAdd.LH_DiaChi = txtDiaChiLH.Text.Trim
                    objKhachHangAdd.LH_DienThoai = txtDienThoaiLH.Text.Trim
                    objKhachHangAdd.LH_Email = txtEmailLH.Text.Trim
                    objKhachHangAdd.InUsed = True
                    objKhachHangAdd.EditedOn = dbTV.GetDateTimeServer
                    dbTV.DM_KhachHangs.InsertOnSubmit(objKhachHangAdd)
                    iSubmitChange()
                Case vEditAction
                    Dim objKhachHangEdit As DM_KhachHang
                    objKhachHangEdit = (From ds In dbTV.DM_KhachHangs Where ds.InUsed AndAlso ds.ID = KhachHangEdit.ID Select ds).FirstOrDefault
                    If objKhachHangEdit IsNot Nothing Then
                        objKhachHangEdit.MaKhachHang = txtMaKhachHang.Text.Trim
                        objKhachHangEdit.MaKhachHang = txtMaKhachHang.Text.Trim
                        objKhachHangEdit.TenKhachHang = txtTenKhachHang.Text.Trim
                        objKhachHangEdit.DiaChi = txtDiaChi.Text.Trim
                        objKhachHangEdit.DienThoai = txtDienThoai.Text.Trim
                        objKhachHangEdit.MaSoThue = txtMaSoThue.Text.Trim
                        objKhachHangEdit.GhiChu = txtGhiChu.Text.Trim
                        objKhachHangEdit.LH_HoTen = txtHoVaTenLH.Text.Trim
                        objKhachHangEdit.LH_DiaChi = txtDiaChiLH.Text.Trim
                        objKhachHangEdit.LH_DienThoai = txtDienThoaiLH.Text.Trim
                        objKhachHangEdit.LH_Email = txtEmailLH.Text.Trim
                        objKhachHangEdit.EditedOn = dbTV.GetDateTimeServer
                        iSubmitChange()
                    End If
            End Select

            If Action = vAddAction Or Action = vCopyAction Then
                InsertTo_lstSourceAllKhachHang(IDKhachHang)
                SaveLog("Cập nhật danh mục khách hàng", "Thêm mới", String.Format("Mã KH: {0}; Tên KH: {1}", txtMaKhachHang.Text.Trim, txtTenKhachHang.Text.Trim), IDKhachHang, "FrmCapNhatKhachHang")
            ElseIf Action = vEditAction Then
                UpdateTo_lstSourceAllKhachHang(IDKhachHang)
                SaveLog("Cập nhật danh mục khách hàng", "Cập nhật", String.Format("Mã cũ: {0}; Mã mới {1}; Tên cũ {2}; Tên mới {3}", KhachHangEdit.MaKhachHang, txtMaKhachHang.Text.Trim, KhachHangEdit.TenKhachHang, txtTenKhachHang.Text.Trim), IDKhachHang, "FrmCapNhatKhachHang")
            End If

        Catch ex As Exception
            MsgErr(ex.ToString)
            Return False
        End Try
        Return True
    End Function

    Private Function CheckSave()
        If String.IsNullOrEmpty(txtMaKhachHang.Text.Trim) Then
            txtMaKhachHang.Focus()
            Return "Bạn chưa nhập mã khách hàng"
        End If
        If String.IsNullOrEmpty(txtTenKhachHang.Text.Trim) Then
            txtTenKhachHang.Focus()
            Return "Bạn chưa nhập tên khách hàng"
        End If
        '// Kiem tra trung ma khach hang
        Select Case Action
            Case vAddAction, vCopyAction
                Dim objKH = (From ds In dbTV.DM_KhachHangs Where ds.InUsed AndAlso ds.MaKhachHang = txtMaKhachHang.Text.Trim Select ds).FirstOrDefault
                If objKH IsNot Nothing Then
                    txtMaKhachHang.Focus()
                    Return "Trùng mã khách hàng"
                End If
            Case vEditAction
                If KhachHangEdit.MaKhachHang <> txtMaKhachHang.Text.Trim Then
                    Dim objKH = (From ds In dbTV.DM_KhachHangs Where ds.InUsed AndAlso ds.MaKhachHang = txtMaKhachHang.Text.Trim Select ds).FirstOrDefault
                    If objKH IsNot Nothing Then
                        txtMaKhachHang.Focus()
                        Return "Trùng mã khách hàng"
                    End If
                End If
        End Select
        Return ""
    End Function

End Class